﻿using System;
using System.Collections.Generic;

namespace ElectronicCommerce.Framework.Caching
{
    /// <summary>
    /// Cache Item
    /// </summary>
    /// <typeparam name="T">T</typeparam>
    public class CacheItem<T>:IComparable<CacheItem<T>>,IComparable<ICacheItem>,IComparer<ICacheItem>,IComparer<CacheItem<T>>,ICacheItem
    {
        #region [  Fileds  ]

        private T obj;

        #endregion

        #region [  Constructors  ]

        private CacheItem() { }

        public CacheItem(string key, T obj)
        {
            this.Key = key;
            this.Created = DateTime.Now;
            this.LastActivity = DateTime.Now;
            this.Frequency = 0;
            this.obj = obj;
        }

        #endregion

        #region [  Properties  ]

        public string Key { get; private set; }

        public DateTime Created { get; private set; }

        public DateTime LastActivity { get; private set; }

        public int Frequency { get; private set; }

        public T Object
        {
            get
            {
                this.LastActivity = DateTime.Now;
                this.Frequency++;
                return obj;
            }
            private set
            {
                this.Created = DateTime.Now;
                this.LastActivity = DateTime.Now;
                this.obj = value;
            }
        }

        #endregion

        #region [  IComparable<CacheItem<T>> Members  ]

        public int CompareTo(CacheItem<T> compareTo)
        {
            if (this.Frequency == compareTo.Frequency)
            {
                return this.LastActivity.CompareTo(compareTo.LastActivity);
            }
            return this.Frequency.CompareTo(compareTo.Frequency);
        }

        #endregion

        #region [  IComparable<ICacheItem> Members  ]

        public int CompareTo(ICacheItem compareTo)
        {
            if (this.Frequency == compareTo.Frequency)
            {
                return this.LastActivity.CompareTo(compareTo.LastActivity);
            }
            return this.Frequency.CompareTo(compareTo.Frequency);
        }

        #endregion

        #region [  IComparer<ICacheItem> Members  ]

        public int Compare(ICacheItem x, ICacheItem y)
        {
            if (x.Frequency == y.Frequency)
            {
                return x.LastActivity.CompareTo(y.LastActivity);
            }
            return x.Frequency.CompareTo(y.Frequency);
        }

        #endregion

        #region [  IComparer<CacheItem<T>> Members  ]

        public int Compare(CacheItem<T> x, CacheItem<T> y)
        {
            if (x.Frequency == y.Frequency)
            {
                return x.LastActivity.CompareTo(y.LastActivity);
            }
            return x.Frequency.CompareTo(y.Frequency);
        }

        #endregion

        #region [  ICacheItem Members  ]

        string ICacheItem.Key
        {
            get { return this.Key; }
        }

        DateTime ICacheItem.Created
        {
            get { return this.Created; }
        }

        DateTime ICacheItem.LastActivity
        {
            get { return this.LastActivity; }
        }

        int ICacheItem.Frequency
        {
            get { return this.Frequency; }
        }

        object ICacheItem.Object
        {
            get { return this.Object; }
        }

        #endregion
    }
}
